﻿using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using models;
using Models;

namespace BLL
{
   public class CartMarager
    {
        CartService service = new CartService();
        //购物车
        //删除购物车内容
        public bool DelCart(string CartID) 
        {
            return service.DelCart(CartID);
        }
        public List<Cart> SelCart(string CartID) 
        {
            List<Cart> carts = new List<Cart>();
            SqlDataReader reader = service.SelCart(CartID);
            while (reader.Read())
            {
                carts.Add(new Cart
                    (
                        reader.GetDecimal(0),//
                        reader.GetInt32(1),//
                        reader.GetInt32(2),//
                        reader.GetInt32(3),//
                        reader.GetString(4),//
                        reader.GetString(5),//
                        reader.GetString(6),//
                        reader.GetDecimal(7),//
                        reader.GetInt32(8),//
                        reader.GetString(9),//
                        reader.GetInt32(10),//
                        reader.GetString(11)//
                        ));
               
            }
            DBhelper.Close();
            return carts;
        }
      
        //增加或减少购物车选择物品的数量
        public bool AddSubCartCount(string id,string cz) 
        {
            return service.AddSubCartCount(id, cz);
        }
        //当有人直接改变TexT的值时直接将Count变了
        public bool UPtextCartCount(string id, string sum) 
        {
            return service.UPtextCartCount(id,sum);
        }
        //全部总价显示
        public Cart SumCart(string id)
        {  
            Cart user =null  ;
            SqlDataReader reader = service.SumCart(id);
            
            while (reader.Read())
            {
                if (reader.IsDBNull(0))
                {
                    user = new Cart(
                        0
                        );
                }
                else
                {
                    user = new Cart(
                  reader.GetDecimal(0)
                   );
                }
                
            }



            DBhelper.Close();
            return user;
        }
        //清空购物车
        public bool DelCartSum(string UserID) 
        {
            return service.DelCartSum(UserID);
        }
        public bool addCart(Cart cart) 
        {
            return service.AddCart(cart);
        }
        public Cart CartCount(string UserId) 
        {
            Cart cart = null;
            SqlDataReader reader = service.CartCount(UserId);

            while (reader.Read())
            {
                if (reader.IsDBNull(0))
                {
                    cart = new Cart(
                        0
                        );
                }
                else
                {
                    cart = new Cart(
                  reader.GetInt32(0)
                   );
                }

            }



            DBhelper.Close();
            return cart;
        }
    }
}
